﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 软著工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.SelectedPath = "D:\\workspace\\TBMS\\tbms";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                DirectoryInfo di = new DirectoryInfo(dialog.SelectedPath);
                checkedListBox1.Items.AddRange(di.GetDirectories());
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (var one in checkedListBox1.CheckedItems)
            {
                if (!checkedListBox2.Items.Contains(one))
                {
                    checkedListBox2.Items.Add(one);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            object[] checkedItems = new object[checkedListBox2.CheckedItems.Count];
            checkedListBox2.CheckedItems.CopyTo(checkedItems, 0);

            foreach (var one in checkedItems)
            {
                checkedListBox2.Items.Remove(one);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            checkedListBox2.Items.Clear();
            richTextBox1.Clear();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();

            List<FileInfo> fis = new List<FileInfo>();
            foreach (var one in checkedListBox2.Items)
            {
                var di = one as DirectoryInfo;
                if (di.Name.StartsWith("."))
                {
                    continue;
                }
                //richTextBox1.AppendText("=>" + di.FullName);
                //richTextBox1.AppendText("\n");

                fis.AddRange(di.GetFiles("*", SearchOption.AllDirectories));
                //fis.AddRange(GetAllFiles(di.GetDirectories()));

                fis = fis.Where(p => (p.Name.ToLower().EndsWith(".cshtml") 
                || p.Name.ToLower().EndsWith(".js") 
                || p.Name.ToLower().EndsWith(".css") 
                || p.Name.ToLower().EndsWith(".vue")
                || p.Name.ToLower().EndsWith(".cs")
                || p.Name.ToLower().EndsWith(".config")
                || p.Name.ToLower().EndsWith(".sql")
                || p.Name.ToLower().EndsWith(".scss")
                || p.Name.ToLower().EndsWith(".sass")
                //|| p.Name.ToLower().EndsWith(".json")
                )
                && !p.FullName.ToLower().Contains(@"\bin\")
                && !p.FullName.ToLower().Contains(@"\obj\")
                && !p.FullName.ToLower().Contains(@"\node_modules\")
                && !p.FullName.ToLower().Contains(@"\dist\")
                && !p.Name.ToLower().Contains(@".min.")
                && !p.FullName.ToLower().Contains(@"\.git\")
                && !p.FullName.ToLower().Contains(@"\.vs\")
                && !p.Name.ToLower().EndsWith(".csproj")
                )
                .ToList();
            }


            // richTextBox1.AppendText("**************************************************************************************************************");

            List<string> allLines = new List<string>();
            int totalLineCount = 0;
            int showLineCount = 0;
            foreach (var one in fis)
            {
                if (showLineCount <= 113 * 60
                    && !one.Name.ToLower().Equals("messagesmanual.designer.cs")
                    && !one.Name.ToLower().Equals("messages.designer.cs")
                    && !one.Name.ToLower().Equals("messages.en.designer.cs")
                    && !one.Name.ToLower().Equals("messages.vi.designer.cs")
                    && !one.Name.ToLower().Equals("messagesmanual.en.designer.cs")
                    && !one.Name.ToLower().Equals("messagesmanual.vi.designer.cs")
                    && !one.Name.ToLower().Equals("pagemessages.designer.cs")
                    && !one.Name.ToLower().Equals("pagemessages.en.designer.cs")
                    && !one.Name.ToLower().Equals("pagemessages.vi.designer.cs")
                    )
                {
                    
                    richTextBox1.AppendText("文件：" + one.FullName.Replace(@"D:\workspace\TBMS\tbms", @"~"));
                    richTextBox1.AppendText("\n");

                    var content = one.OpenText();
                    for (string lineContenct = string.Empty; lineContenct != null; lineContenct = content.ReadLine())
                    {
                        totalLineCount++;
                        showLineCount++;

                        if (!string.IsNullOrWhiteSpace(lineContenct.Trim()))
                        {
                            richTextBox1.AppendText(lineContenct);
                            richTextBox1.AppendText("\n");
                        }
                    }
                }
                else
                {
                    var content = one.OpenText();
                    for (string lineContenct = string.Empty; lineContenct != null; lineContenct = content.ReadLine())
                    {
                        totalLineCount++;
                    }
                }
            }

            richTextBox1.AppendText(totalLineCount + "\n");
            richTextBox1.AppendText(showLineCount + "");
        }

        private List<FileInfo> GetAllFiles(DirectoryInfo[] directories)
        {
            List<FileInfo> fis = new List<FileInfo>();
            foreach (var one in directories)
            {
                fis.AddRange(one.GetFiles());

                fis.AddRange(GetAllFiles(one.GetDirectories()));
            }

            return fis;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Clear();
            checkedListBox2.Items.Clear();
            richTextBox1.Clear();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            object[] checkedItems = new object[checkedListBox1.CheckedItems.Count];
            checkedListBox1.CheckedItems.CopyTo(checkedItems, 0);

            foreach (var one in checkedItems)
            {
                checkedListBox1.Items.Remove(one);
            }
        }
    }
}
